package controller.admin;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import pojos.common.ReplyMessage;
import pojos.teacher.TeacherInfo;
import service.TeacherInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;

/**
 * 教师控制相关控制器
 */
@Controller
@RequestMapping("/admin")
public class AdminForTeacherController {

    @Resource(name = "teacherInfoService")
    private TeacherInfoService teacherInfoService;

    @PostMapping("/frame/addTeacher-submit")
    @ResponseBody
    public ReplyMessage addTeacher(@RequestParam(value = "loginName",required = false) String loginName,
                                   @RequestParam(value = "loginPswd",required = false) String loginPswd,
                                   @RequestParam(value = "phoneNum", required = false) String phoneNum,
                                   @RequestParam(value = "emailNum", required = false) String emailNum,
                                   @RequestParam(value = "userName",required = false) String userName,
                                   @RequestParam(value = "userSex",required = false) Integer userSex,
                                   @RequestParam(value = "QQ",required = false) String QQ,
                                   @RequestParam(value = "wechat",required = false) String weChat,
                                   @RequestParam(value = "school",required = false) String school,
                                   @RequestParam(value = "major",required = false) String major,
                                   @RequestParam(value = "edu",required = false) Integer edu,
                                   @RequestParam(value = "grade",required = false) Integer grade,
                                   @RequestParam(value = "comment",required = false) String comment){
        ReplyMessage replyMessage = new ReplyMessage();
        TeacherInfo teacherInfo = new TeacherInfo();

        // 登录名验证
        if(loginName == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名称不能为空！");
            return replyMessage;
        }
        int loginNameLen = loginName.length();
        if(loginNameLen < 2 || loginNameLen > 20){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名长度不正确！");
            return replyMessage;
        }

        // 登录密码验证
        if(loginPswd == null){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不能为空！");
            return replyMessage;
        }
        if(loginPswd.length() != 32){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录密码不正确！");
            return replyMessage;
        }

        // 手机号验证
        if(phoneNum != null && phoneNum.length() > 0){
            if(InputVerifyUtils.isPhoneNotRight(phoneNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("手机号不正确！");
                return replyMessage;
            }
            teacherInfo.setPhone(phoneNum);
        }

        // 邮箱验证
        if(emailNum != null && emailNum.length() > 0){
            if(InputVerifyUtils.isEmailNotRight(emailNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("邮箱号不正确！");
                return replyMessage;
            }
            teacherInfo.setEmail(emailNum);
        }

        // 真实姓名验证
        if(userName != null && userName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(userName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("用户真实姓名不正确！");
                return replyMessage;
            }
            teacherInfo.setRealName(userName);
        }

        // 性别判断
        if(userSex != null){
            if(userSex == 1){
                teacherInfo.setSex((byte) 1);
            }else{
                teacherInfo.setSex((byte) 0);
            }
        }

        // QQ号判断
        if(QQ != null && QQ.length() > 0){
            if(InputVerifyUtils.isQQNotRight(QQ)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号不正确！");
                return replyMessage;
            }
            teacherInfo.setQQ(QQ);
        }

        // 微信号判断
        if(weChat != null && weChat.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChat)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确");
                return replyMessage;
            }
            teacherInfo.setWeChat(weChat);
        }

        // 学校验证
        if(school != null && school.length() > 0){
            if(InputVerifyUtils.isSchoolNotRight(school)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("学校名称不正确！");
                return replyMessage;
            }
            teacherInfo.setSchool(school);
        }

        // 专业验证
        if(major != null && major.length() > 0){
            if(InputVerifyUtils.isMajorNotRight(major)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("学校名称不正确！");
                return replyMessage;
            }
            teacherInfo.setSchool(major);
        }

        // 学历判断
        Integer eduInt = InputVerifyUtils.getRightEdu(edu);
        if(eduInt != null){
            teacherInfo.setEdu(eduInt);
        }

        // 教师年级判断
        Integer gradeInt = InputVerifyUtils.getTeacherGrade(grade);
        if(gradeInt != null){
            teacherInfo.setGrade(gradeInt);
        }

        // 备注验证
        if(comment != null){
            if(comment.length() > 85){
                replyMessage.setStatus(400);
                replyMessage.setInfo("备注不允许超过85个字符！");
                return replyMessage;
            }

            teacherInfo.setComment(comment);
        }

        teacherInfo.setName(loginName);
        teacherInfo.setPassword(loginPswd);
        return teacherInfoService.adminAddTeacher(teacherInfo);
    }

    /**
     * 查找所有的教师登录名
     */
    @PostMapping("/frame/queryAllTeacherName")
    @ResponseBody
    public ReplyMessage queryAllTeacherName(){
        return teacherInfoService.queryAllTeacherName();
    }
}
